package 题目集.bfs;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * https://www.acwing.com/problem/content/1564/
 */
public class 微博转发 {
    static List<Integer>[] graph;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        graph=new List[n+1];
        for (int i = 1; i < graph.length; i++) {
            graph[i]=new ArrayList<>();
        }
        for (int i = 1; i <= n; i++) {
            int k = sc.nextInt();
            while (k-->0){
                int next = sc.nextInt();
                graph[next].add(i);
            }
        }
        int t = sc.nextInt();
        while (t-->0){
            int start = sc.nextInt();
            int res = bfs(start, m);
            System.out.println(res);
        }
    }
    static int[] queue=new int[1001];
    public static int bfs(int s,int m){
        int h=0,t=0;
        boolean[] vis=new boolean[1001];
        queue[t++]=s;
        int res=0;
        int level=0;
        vis[s]=true;
        while (h<t){
            if (level>=m){
                break;
            }
            level++;
            int size = t - h;
            res+=size;
            for (int i = 0; i < size; i++) {
                int cur = queue[h++];
                for (Integer to : graph[cur]) {
                    if(!vis[to]){
                        vis[to]=true;
                        queue[t++]=to;
                    }
                }
            }
        }
        return res+t-h-1;   //加上最后一层的节点数，以及减掉起点
    }
}
